
#include "SvrNetSessionMan.h"
#include "TGSvrNetMan.h"


SvrNetSessionMan::SvrNetSessionMan(void)
{
	m_pSvrNetMan = NULL;

	SessionHead  = NULL;;
	SessionTail  = NULL;;

	
}

SvrNetSessionMan::~SvrNetSessionMan(void)
{
	UnInitialize();
}
int SvrNetSessionMan::SetSvrNetMan(TGSvrNetMan *pSvrNet)
{
	m_pSvrNetMan = pSvrNet;

	//InitializeNetCmd();

	return 0;
}

int SvrNetSessionMan::UnInitialize()
{
	if( m_pSvrNetMan == NULL )
	{
		return 0 ;	
	}
	UnInitializeNetCmd();

	DD_AUTO_LOCK xLock(&m_xSessionSect);
	SessionTail = SessionHead ;
	while (SessionTail != NULL)
	{
		SessionHead = SessionHead->next ;
		MySafeDelete(SessionTail);
		SessionTail = SessionHead ;			
	}
	m_pSvrNetMan = NULL ;
	return 0;
}


void SvrNetSessionMan::UnInitializeNetCmd()
{
	DDThread::m_bThreadRun = false;
	DDThread::DDThdUnInit();

	
}

void SvrNetSessionMan::InitializeNetCmd()
{
	NET_PACKET_NODE *pPacketList = &(m_xNetCommand[0]);

	UINT32 nLast = TG_SVNNET_MAXCOMMAND - 1;
	//next
	UINT32 i = 0;
	for(i=0;i<nLast;i++)
	{
		pPacketList[i].pNext      = &pPacketList[i+1];
		pPacketList[i].nPacketID  = i;
		pPacketList[i].nWriteFlag = 1;
	}
	pPacketList[nLast].pNext     = &pPacketList[0];
	pPacketList[nLast].nPacketID = nLast;
	pPacketList[nLast].nWriteFlag = 1;
	//prev
	for(i=1;i<TG_SVNNET_MAXCOMMAND;i++)
	{
		pPacketList[i].pPrev = &pPacketList[i-1];
	}
	pPacketList[0].pPrev = &pPacketList[nLast];

	m_pWriteCmdNode = pPacketList;
	m_pReadCmdNode  = NULL;



	DDThread::DDThdInit();
}



int SvrNetSessionMan::SetCommand(NET_PACKET *pPacket)
{
 	m_pWriteCmdNode->xPacket.xPacketHead = pPacket->xPacketHead;
	if( pPacket->xPacketHead.nFrameSize > 0 )
	{
       memcpy( m_pWriteCmdNode->xPacket.cPacketData,pPacket->cPacketData,pPacket->xPacketHead.nFrameSize );
	}
	m_pWriteCmdNode->nWriteFlag = 0;

	if( NULL == m_pReadCmdNode )
	{
		m_pReadCmdNode = m_pWriteCmdNode;
	}

	m_pWriteCmdNode = m_pWriteCmdNode->pNext;

	//SetEvent( m_hCommandEvent );

	return TG_ERR_SUCCESS;
}



int SvrNetSessionMan::TGThdDispatch()
{


// 	SetThreadPriority(GetCurrentThread() , THREAD_PRIORITY_ABOVE_NORMAL);
// 
// 	while( TGThread::m_bThreadRun )
// 	{
// 		WaitForSingleObject( m_hCommandEvent, INFINITE );
// 		if( !TGThread::m_bThreadRun )
// 		{
// 			break;
// 		}	
// 
// 		DoCommand();
// 	}



	return 0;
}

inline void SvrNetSessionMan::DoCommand()
{
	/*
	if( NULL == m_pReadCmdNode )
	{
		m_pReadCmdNode = m_pWriteCmdNode->pPrev;
	}
	if( m_pReadCmdNode->nWriteFlag )
	{
		ResetEvent( m_hCommandEvent );
		return;
	}

	int nRet = TG_ERR_SUCCESS;

	NET_PACKET *pPacket = &(m_pReadCmdNode->xPacket);
	switch( pPacket->xPacketHead.nMsgType )
	{
	case MSG_TYPE_TGD_CLIENT_ALIVE:
		{
			NET_SVR_RS_CONTEXT *pContext = (NET_SVR_RS_CONTEXT*)( pPacket->cPacketData );

			UINT16 nSessionPos = pContext->nSvrRsCameraPos;

// 			if( 0 <= nSessionPos && nSessionPos < TG_SVRNET_MAXCHANNEL )
// 			{
// 				TG_AUTO_LOCK xLock(&m_xSessionSect[nSessionPos]);
// 
// 				SvrNetRPSession *pSession = m_arSession[nSessionPos];
// 				if( NULL != pSession )
// 				{
// 					pSession->KeepAlive(pContext);
// 				}
// 			}
		}
		break;
	case MSG_TYPE_TGD_RETRANS_PACKET:
	case MSG_TYPE_TGD_CHANGE_NET_STATE:
		{
			NET_SVR_RS_CONTEXT *pContext = (NET_SVR_RS_CONTEXT*)( pPacket->cPacketData );
			NET_RETRANS_PACKET *pRetrans = (NET_RETRANS_PACKET*)( pPacket->cPacketData + sizeof(NET_SVR_RS_CONTEXT) );

			UINT16 nSessionPos = pContext->nSvrRsCameraPos;
			if( 0 <= nSessionPos && nSessionPos < TG_SVRNET_MAXCHANNEL )
			{
// 				TG_AUTO_LOCK xLock(&m_xSessionSect[nSessionPos]);
// 
// 				SvrNetRPSession *pSession = m_arSession[nSessionPos];
// 				if( NULL != pSession )
// 				{
// 					pSession->SetCmdRetransPacket(pContext,pPacket);
// 				}
			}
		}
		break;
	default:
		{
			break;
		} 
	}

	m_pReadCmdNode->nWriteFlag = 1;
	m_pReadCmdNode = m_pReadCmdNode->pNext;
	*/
}



int SvrNetSessionMan::CreateConnection(NET_PACKET_HEAD *pRetMsgHead,TG_CONNECT_INFO *pConnectInfo,SOCKET hSocket,TG_UDP_ADDR *pAddr )
{
	if( NULL == pRetMsgHead   ||
		NULL == pConnectInfo  )
	{
		return TG_ERR_PARAMETER_ERROR;
	}

	int nRet = TG_ERR_FAILURE;

	try
	{
		DD_AUTO_LOCK xLock(&m_xSessionSect);
		SvrNetRPSession*  pSession = SessionHead ;
		while (NULL != pSession )
		{
			if( pSession->m_bUse && pConnectInfo->xAnchorInfo.dwUserID == pSession->GetAnchorID() )
			{
				break;
			}
			pSession = pSession->next ;
		}
	
		if( NULL == pSession )
		{
			return TG_ERR_CHANNEL_ERROR;
		}

		return pSession->AddItem(pRetMsgHead,pConnectInfo,hSocket,pAddr);

	}
	catch (...)
	{
	}

	return nRet;
}